﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Xml;

namespace LbangCMS.Areas.WeChat.Controllers
{
    public class PayController : Controller
    {
        // GET: WeChat/Pay
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Notify(FormCollection form)
        {
            StreamReader reader = new StreamReader(Request.InputStream, Encoding.UTF8);
            string postStr = reader.ReadToEnd();
            reader.Close();
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(postStr);
            string ReturnCode = doc.SelectSingleNode("//result_code").InnerText;//获取支付结果代码
            StringBuilder xmlStr = new StringBuilder();
            //TODO 签名认证，防止假成功
            if (ReturnCode.ToUpper().Equals("SUCCESS"))
            {
                try
                {
                    //获取订单号
                    string strOrderNo = doc.SelectSingleNode("//out_trade_no").InnerText;
                    //获取微信支付流水号
                    string weChatTransaction_Id = doc.SelectSingleNode("//transaction_id").InnerText;

                    //var chargeOrder = _chargeService.GetChargeOrder(strOrderNo);
                    //if (chargeOrder != null && chargeOrder.Status == 0)
                    //{
                    //    var result = _chargeService.SaveCallbackInfo(strOrderNo, weChatTransaction_Id, postStr);
                    //    if (result)
                    //    {
                    //        //TODO 写日志
                    //        var updateMoneyResult = _memberService.IncreaseMoney(chargeOrder.UserId, chargeOrder.Money, chargeOrder.OrderNo, weChatTransaction_Id, "微信充值成功" + chargeOrder.Money.ToString("0.00") + "元", "微信流水号:" + weChatTransaction_Id);
                    //        if (updateMoneyResult)
                    //        {
                    //            var rechargeCouponPromotion = _promotionService.GetReChargeCouponPromotion(chargeOrder.Money);
                    //            if (rechargeCouponPromotion != null)
                    //            {
                    //                var coupon = _couponService.GetCoupon(rechargeCouponPromotion.GiveConponId);
                    //                if (coupon.ExpireTime > DateTime.Now)
                    //                {
                    //                    _couponService.SaveUserCoupon(chargeOrder.UserId, coupon.Id);
                    //                }
                    //            }
                    //            var rechargeCreditPromotion = _promotionService.GetReChargeCreditPromotion(chargeOrder.Money);
                    //            if (rechargeCreditPromotion != null)
                    //            {
                    //                _memberService.IncreaseCredit(chargeOrder.UserId, rechargeCreditPromotion.GiveCredit, "充值送积分", rechargeCreditPromotion.Name);
                    //            }
                    //        }

                    //        #region 通知微信后台接收通知成功
                    //        Response.ContentType = "text/xml";
                    //        xmlStr.AppendLine("<xml>");
                    //        xmlStr.AppendFormat("<return_code><![CDATA[SUCCESS]]></return_code>");
                    //        xmlStr.AppendFormat("<return_msg><![CDATA[OK]]></return_msg>");
                    //        xmlStr.AppendFormat("</xml>");
                    //        return Content(xmlStr.ToString());
                    //        #endregion
                    //    }
                    //}
                    //else if (chargeOrder != null && chargeOrder.Status == 1)
                    //{
                    //    #region 通知微信后台接收通知成功
                    //    Response.ContentType = "text/xml";
                    //    xmlStr.AppendLine("<xml>");
                    //    xmlStr.AppendFormat("<return_code><![CDATA[SUCCESS]]></return_code>");
                    //    xmlStr.AppendFormat("<return_msg><![CDATA[OK]]></return_msg>");
                    //    xmlStr.AppendFormat("</xml>");
                    //    #endregion
                    //    return Content(xmlStr.ToString());
                    //}
                }
                catch (Exception ex)
                {
                    //记录错误
                    throw ex;
                }

            }

            #region 通知微信后台接收通知失败
            Response.ContentType = "text/xml";
            xmlStr.AppendLine("<xml>");
            xmlStr.AppendFormat("<return_code><![CDATA[FAIL]]></return_code>");
            xmlStr.AppendFormat("<return_msg><![CDATA[支付失败]]></return_msg>");
            xmlStr.AppendFormat("</xml>");
            #endregion
            return Content(xmlStr.ToString());
        }
    }
}